﻿using Projet_IMA.Base;
using Projet_IMA.Light;
using Projet_IMA.Object3D;
using Projet_IMA.Projections;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Projet_IMA.Habillage.Material;

namespace Projet_IMA.Environnement
{
    public partial class ControllerForm : Form
    {
        private List<View3D> Views3D;
        public Scene3D Scene3D;

        public ControllerForm()
        {
            InitializeComponent();

            this.Views3D = new List<View3D>();
            this.Scene3D = new Scene3D();

            this.LoadTestScene();
            this.ChangeViewMode(EModeView.F);
        }

        private void BTN_ViewMode_GD_Click(object sender, EventArgs e)
        {
            ChangeViewMode(EModeView.GD);
        }

        private void BTN_ViewMode_F_Click(object sender, EventArgs e)
        {
            ChangeViewMode(EModeView.F);
        }

        private void BTN_ViewMode_FGD_Click(object sender, EventArgs e)
        {
            ChangeViewMode(EModeView.FGD);
        }

        public void LoadTestScene()
        {
            
            MaterialBase m1 = new MaterialTexture(new Habillage.TextureImage("rock.jpg")); //MaterialColor(new Couleur(0.9f, 0.7f, 0.0f));
            MaterialBase m2 = new MaterialTexture(new Habillage.TextureImage("wood.jpg")); //new MaterialColor(new Couleur(0.5f, 0.2f, 0.7f));
            MaterialBase m3 = new MaterialTexture(new Habillage.TextureImage("brick01.jpg"));//new MaterialColor(new Couleur(0.3f, 0.5f, 0.7f));
            MaterialBase m4 = new MaterialTexture(new Habillage.TextureImage("carreau.bmp"));//new MaterialColor(new Couleur(0.5f, 0.2f, 0.7f));
            this.Scene3D.AddMaterial(m1);
            this.Scene3D.AddMaterial(m2);
            this.Scene3D.AddMaterial(m3);
            this.Scene3D.AddMaterial(m4);

            this.Scene3D.AddObject(new Spheres(m1, new V3(-100, 0, -100), 100, 0.009F));
            this.Scene3D.AddObject(new Spheres(m1, new V3(50, 0, -100), 100, 0.009F));

            this.Scene3D.AddObject(new Parallelogramme(new V3(-70, 10, -100), new V3(30, 10, -100), new V3(-70, 10, 200), m2, 0.003F));
            this.Scene3D.AddObject(new Spheres(m3, new V3(-20, 0, 190), 50, 0.01F));

            this.Scene3D.AddObject(new Triangle(new V3(200, 0, -50), new V3(300, 50, 10), new V3(230, 500, 100), m4, 0.002F));

            this.Scene3D.AddLight(new LightDirectional(new V3(1.0f, -1.0f, 1.0f), new Couleur(1.0f, 1.0f, 1.0f)));
            //this.Scene3D.AddLight(new LightDiffuse(new V3(1.0f, -1.0f, -1.0f), new Couleur(1.0f, 1.0f, 1.0f)));

            

            //--------------------------------------------bump


            MaterialBase m8 = new MaterialTexture(new Habillage.TextureImage("gold.jpg"));
            this.Scene3D.AddMaterial(m8);
            m8.SetBump(new Habillage.TextureBump("bump38.jpg", 0.01F));

            this.Scene3D.AddObject(new Spheres(m8, new V3(200, 200, 0), 100, 0.005F));

            /*
            MaterialBase m1 = new MaterialColor(new Couleur(0.9f, 0.7f, 0.0f));//new MaterialTexture(new Habillage.TextureImage("gold.jpg"));
            this.Scene3D.AddMaterial(m1);
            m1.SetBump(new Habillage.TextureBump("bump38.jpg", 0.05F));

            MaterialBase m2 = new MaterialTexture(new Habillage.TextureImage("gold.jpg"));//new MaterialTexture(new Habillage.TextureImage("brick01.jpg"));//new MaterialTexture(new Habillage.TextureImage("gold.jpg"));
            this.Scene3D.AddMaterial(m2);
            m2.SetBump(new Habillage.TextureBump("bump-none.png", 0.01F));

            this.Scene3D.AddObject(new Parallelogramme(new V3(-70, -10, -100), new V3(200, -10, -100), new V3(-70, -10, 200), m2, 0.002F));
            this.Scene3D.AddObject(new Triangle(new V3(200, 0, 10), new V3(300, 50, 10), new V3(230, 400, 100), m1, 0.001F));
            this.Scene3D.AddObject(new Spheres(m2, new V3(-100, 0, -100), 100, 0.005F));
            this.Scene3D.AddLight(new LightDiffuse(new V3(1.0f, -1.0f, 1.0f), new Couleur(1.0f, 1.0f, 1.0f)));
            */

            // ---------------------------------------  Specular

            MaterialBase m5 = new MaterialColor(new Couleur(0.9f, 0.7f, 0.0f));
            MaterialBase m6 = new MaterialColor(new Couleur(0.9f, 0.7f, 0.0f));
            MaterialBase m7 = new MaterialColor(new Couleur(0.9f, 0.7f, 0.0f));
            this.Scene3D.AddMaterial(m5);
            this.Scene3D.AddMaterial(m6);
            this.Scene3D.AddMaterial(m7);

            m5.AsSpecular = false;
            m6.AsDiffus = false;

            this.Scene3D.AddObject(new Ligne(new V3(350, 200, 275), new V3(0, 0, -1), 50, new Couleur(0f, 0f, 0f), 1.0f));
            this.Scene3D.AddObject(new Ligne(new V3(325, 200, 250), new V3(1, 0, 0), 50, new Couleur(0f, 0f, 0f), 1.0f));

            this.Scene3D.AddObject(new Ligne(new V3(325, 200, 165), new V3(1, 0, 0), 50, new Couleur(0f, 0f, 0f), 1.0f));
            this.Scene3D.AddObject(new Ligne(new V3(325, 200, 200), new V3(1, 0, 0), 50, new Couleur(0f, 0f, 0f), 1.0f));

            this.Scene3D.AddObject(new Spheres(m5, new V3(250, 200, 250), 50, 0.01F));
            this.Scene3D.AddObject(new Spheres(m6, new V3(450, 200, 250), 50, 0.01F));
            this.Scene3D.AddObject(new Spheres(m7, new V3(350, 200, 100), 50, 0.01F));

            //this.Scene3D.AddLight(new LightDirectional(new V3(1.0f, -1.0f, 1.0f), new Couleur(1.0f, 1.0f, 1.0f)));

        }

        public void ChangeViewMode(EModeView viewMode)
        {
            this.Views3D.Clear();
            this.tableLayoutPanel1.Controls.Clear();

            switch (viewMode)
            {
                case EModeView.F:
                    this.tableLayoutPanel1.RowCount = 1;
                    this.tableLayoutPanel1.ColumnCount = 1;
                    this.AddView(0, 0, 1, EProjection.OrthogonaleFace);
                    break;
                case EModeView.GD:
                    this.tableLayoutPanel1.RowCount = 1;
                    this.tableLayoutPanel1.ColumnCount = 2;
                    this.AddView(0, 0, 1, EProjection.OrthogonaleLeft);
                    this.AddView(0, 1, 1, EProjection.OrthogonaleRight);
                    break;
                case EModeView.FGD:
                    this.tableLayoutPanel1.RowCount = 2;
                    this.tableLayoutPanel1.ColumnCount = 2;
                    this.AddView(0, 0, 2, EProjection.OrthogonaleFace);
                    this.AddView(0, 1, 1, EProjection.OrthogonaleLeft);
                    this.AddView(1, 1, 1, EProjection.OrthogonaleRight);
                    break;
            }
        }

        private void AddView(int row, int col, int colSpan, EProjection projection)
        {
            View3D view = new View3D(Scene3D, projection);
            view.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Views3D.Add(view);

            this.tableLayoutPanel1.Controls.Add(view, col, row);
            this.tableLayoutPanel1.SetColumnSpan(view, colSpan);
        }

        private void ReDraw()
        {
            //DateTime d = DateTime.Now;

            foreach (View3D view in this.Views3D)
                view.Draw();

            //TimeSpan dt = DateTime.Now - d;
            //Console.WriteLine(dt.Milliseconds);
        }

        private void BTN_IncreasesRepere_Click(object sender, EventArgs e)
        {
            Scene3D.IncreasesRepere();
            this.ReDraw();
        }

        private void BTN_DecreasesRepere_Click(object sender, EventArgs e)
        {
            Scene3D.DecreasesRepere();
            this.ReDraw();
        }
    }
}
